var EpgWidgetEpisode = Class.create(EpgWidget,{
	initialize: function($super, id, opts,links) {
        $super(id,opts,links);
        this.epiTabs = opts.epiTabs;
        this.selectIndex = opts.selectIndex;
        this.selectClass = opts.selectClass;
        this.sort = opts.sort; //正序:asc,倒序:desc;
		this.episodes = new Array(opts.totalRecordCount); 
		this.init(opts.episodes);
		
		this.regions = this.getRegion();
	    if(this.isDesc()){
		   	this.reverse();
		   	this.renderTabs();
	    }
	    this.pageNo = opts.pageNo;
	    this.refreshEpi(this.pageNo-1);
    },
	getRegion:function(){
		var result = new Array();
		
		var count =  this.opts.totalRecordCount;
		var mod = count % this.opts.pageSize;
    	var rCount = (count-mod) / this.opts.pageSize;
		var lastCount = this.opts.pageSize;
		if(mod>0) {
			lastCount = mod;
			rCount = rCount+1;
		}
		for(var i=0;i<rCount-1;i++){
			var nbegin = i*this.opts.pageSize;
			var nend = (i+1)*this.opts.pageSize-1;
			result.push({begin:nbegin,end:nend});
		}
		result.push({begin:count-lastCount,end: count-1});
		return result;
    },
    isDesc:function(){
		return this.sort=='desc';
	},
	init:function(episodes){
	
		for(var i = 0; i < episodes.length; i++) {
        	var epi = episodes[i];
        	this.episodes[epi.episodeIndex-1] = epi;
        }
		for(var i=0;i<this.episodes.length;i++){
			var epi = this.episodes[i];
				if(epi){
					epi["enable"] = true;
				}else{
					this.episodes[i] = {episodeIndex:i+1,enable:false}
				}
		}
	},
	reverse:function(){
		var count = this.opts.totalRecordCount;
		var result = new Array();
		for(var i=0;i<count;i++){
			result[i] = this.episodes[count-i-1];
		}
		this.episodes = result;
		this.regions = this.getRegion();
	},
    episodeSort: function(sortFlag) {
    	if(this.sort != sortFlag) {
			this.sort = sortFlag;
			this.reverse();
    		this.renderTabs();
			this.refreshEpi(0);
    	}
		if(this.sort=='desc'){
				$("#episode_link_zx2dx_1_text").addClass('zx2dx_episode current_episode');
    			$("#episode_link_zx2dx_0_text").removeClass('current_episode');
    	}else{
    			$("#episode_link_zx2dx_0_text").addClass('zx2dx_episode current_episode');
    			$("#episode_link_zx2dx_1_text").removeClass('current_episode');
		}

    },
    renderTabs: function() {
	   	var epiTabsLinks = this.epiTabs.links;
    	for(var i = 0; i < this.regions.length; i++) {
			var region = this.regions[i];
			var bEpi = this.episodes[region.begin];
			var eEpi = this.episodes[region.end];
    		var title = (bEpi.episodeIndex+"-" + eEpi.episodeIndex);
			$("#episode_link_multiple_"+i+"_text").html(title);
    		
    	}
    },
	renderEpisode:function(linkId,epi,focus,index){
			
        	$("#"+linkId+"_layout").css("display","block");
        	$("#"+linkId+"_text").html(epi.episodeIndex);
        	var link = epgPage.findLink(linkId);
        	if(epi.enable) {
    			$("#"+linkId+"_text").attr("class","single_episode");
        		link.href = "javascript:"+this.id+"Widget.playFull("+index+",'"+link.id+"');";
        		link.disable = false;
        	} else {
        		$("#"+linkId+"_text").attr("class","disable_episode");
        		link.disable = true;
        	}
        	if(focus) {
    			$("#"+linkId+"_text").addClass('current_episode');
    		} else {
    			$("#"+linkId+"_text").removeClass('current_episode');
    		}
	},
	refreshEpi:function(tabidx) {
		this.pageNo = tabidx+1; //赋值给this.pageNo是因为，要通过ps传出去并带回来
		var epiTabsLinks = this.epiTabs.links;
		for(var i=0;i<epiTabsLinks.length;i++) {
			if(tabidx==i) {
				$("#"+epiTabsLinks[i]+"_text").addClass('multiple_episode current_episode');
			} else {
				$("#"+epiTabsLinks[i]+"_text").removeClass('current_episode');
			}
		}
		var region = this.regions[tabidx];
		var idx =0;
		for(var i=region.begin;i <= region.end;i++){
				idx = i-region.begin;
				var aLink = this.links[idx]
				var epi = this.episodes[i];
				var focus = false;
				if(epi.enable) {
					var tempIndex = i;
					if(this.isDesc()){
						tempIndex = this.episodes[i].episodeIndex-1;
					}
					if(this.selectIndex==tempIndex) {
						focus = true;
					}
				}
				this.renderEpisode(aLink,epi,focus,i);
		}
		idx++;
        for( ;idx<this.opts.pageSize; idx++) {
        	var link = epgPage.findLink(this.links[idx]);
        	$("#"+this.links[idx]+"_layout").css("display","none");
        	$("#"+this.links[idx]+"_text").html('');
        	link.href = "#";
        	link.disable = true;
        }
	},
    playFull: function (index,id) { //index集数索引
    	var epi = this.episodes[index];
    	//页数,某页某条(剧集),排序[正序/倒序],当前导航
    	var ps = this.pageNo+','+index+','+this.sort;
    	if(this.opts.play) {
    		ps = this.opts.play(ps,epi);
    	}
    	url = epi.url+'?lps='+ps;
    	gotoPlayByBookMark(url,id,epi.contentCode);
    },
    focus: function(id) {
    	var link = epgPage.findLink(id);
    	if(link.disable) {
    		for(var i=0; i<this.links.length;i++) {
    			var link = epgPage.findLink(this.links[i]);
    			if(!link.disable) {
    				epgPage.focus(link);
    				return true;
    			}
    		}
    	}
    	return false;
    },
    getPageNo: function() {
    	return this.pageNo;
    },
    getSort: function() {
    	return this.sort;
    }
}); 